import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getPoster,deletePoster } from '../services/index';
import {IPosterItem} from "../utils/poster"
export interface IndexModelState {
  name: string;
  poster: Array<IPosterItem>,
  remote:[]

}

export interface IndexModelType {
  namespace: 'poster';
  state: IndexModelState;
  effects: {
    getPoster: Effect;
    deletePoster: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}



const IndexModel: IndexModelType = {
  namespace: 'poster',

  state: {
    name: '',
    poster: [],
    remote:[]
  },

  //异步action
  effects: {
    *getPoster({type, payload }, { call, put }) {
      let results = yield call(getPoster, payload);
      yield put({
        type: 'save',
        payload: {
          poster: results.data[0]
        }
      })
    },
    *deletePoster({type, payload }, { call, put }) {
      let results = yield call(deletePoster,payload, 200);
      yield put({
        type: 'save',
        payload: {
          remote: results
        }
      })
    },
  },
  //同步action
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },

};

export default IndexModel;
